package sicnu.cs.ich.ich.ranking.mq;

import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.stereotype.Component;
import sicnu.cs.ich.api.domain.mq.IchMQ;
import sicnu.cs.ich.ich.ranking.service.IIchRankingService;
import sicnu.cs.ich.rocketmq.rocketmq.AbstractRocketConsumer;

import java.lang.reflect.InvocationTargetException;

/**
 * @author CaiKe
 * @date 2021/12/2 15:54
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class IchRankingConsumer extends AbstractRocketConsumer {

    private final IIchRankingService rankingService;


    @Override
    public void init() {
        super.necessary("ich", "*", "非遗排行榜消费");
        handleMessage();
    }

    public ConsumeConcurrentlyStatus ich(MessageExt message){
        final var ich = (Integer) JSON.parse(message.getBody());
        rankingService.ichHitIncrement(ich);
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }

    public ConsumeConcurrentlyStatus province(MessageExt message){
        final IchMQ ich = JSON.parseObject(message.getBody(), IchMQ.class);
        rankingService.ichProvinceIncrement(ich);
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }

    public ConsumeConcurrentlyStatus type(MessageExt message){
        final Integer ichId = (Integer) JSON.parse(message.getBody());
        rankingService.ichTypeHitIncrement(ichId);
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }

    public ConsumeConcurrentlyStatus support(MessageExt message){
        final Integer typeId = (Integer) JSON.parse(message.getBody());
        rankingService.updateIchHit(typeId);
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }

}

